//
//  ZLQSaveToCaches.swift
//  GoTravel
//
//  Created by lanou3g on 16/3/11.
//  Copyright © 2016年 SHH. All rights reserved.
//

import UIKit

class ZLQSaveToCaches: NSObject {
    
    var userDataArray = Array<UserArrayModel>()
    
    
    private override init() {}
    static var sharedImage:ZLQSaveToCaches? = ZLQSaveToCaches()
    
    func getUserInfo()->ZLQSaveToCaches {
        let user = AVUser.currentUser()
        let query = AVQuery(className: "UserInfo")
        query.whereKey("user_id", equalTo: user["user_id"])
        let userArray = query.findObjects()
        if userArray.isEmpty || userArray == nil {
            print("数组没有纸")
        }else{
            let resultUser = userArray.first
            ZLQSaveToCaches.sharedImage!.userDataArray = (resultUser!["user_array"] as? Array<UserArrayModel>)!
            
        }
        return ZLQSaveToCaches.sharedImage!
    }
    
    
    func updateUserInfo(array:Array<UserArrayModel>) {
        let user = AVUser.currentUser()
        let query = AVQuery(className: "UserInfo")
        query.whereKey("user_id", equalTo: user["user_id"])
//        let userArray = query.findObjects()
        let object:AVObject = query.findObjects().first! as! AVObject
//        object?.setValue(array, forKey: "user_array")
        object.setObject(array, forKey: "user_array")
        object.saveInBackgroundWithBlock({ (succeeded:Bool, error:NSError!) -> Void in
            
            if succeeded {
                print("成功")
            }else{
                print("失败")
                
            }
            
        })
    }
    
}
